package com.csist.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/**
 * @author YF
 * @date 2024/4/25 16:29
 */
public class SqlSessionUtil {
    private static SqlSessionFactory build = null;

    private SqlSessionUtil() {

    }

    static {
        try {
            build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @return 返回链接对象
     */
    public static SqlSession getSqlsession() {
        return build.openSession();
    }

    /**
     * 关闭链接对象
     *
     * @param sqlSession
     */
    public static void close(SqlSession sqlSession) {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

}
